package LogicSide;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DBside.JDBCConnection;
import DBside.JDBCConnection.Table;
import DBside.TableItem;
import DBside.Tools;

public class FollowersModifier {
	/*
	 * public static void main(String[] args) throws SQLException {
	 * JDBCConnection con = new JDBCConnection();
	 * con.openConnection(Tools.LocalURL, Tools.LocaluserName,
	 * Tools.Localpassword); Session s=new Session(); s.setCon(con);
	 * System.out.println(addFollow("avigdor","zion",s));
	 * //System.out.println(removeFollow("shlomo","shlomo", s)); }
	 */
	/**
	 * 
	 * @param follow_name
	 * @param s
	 * @return id of user which the user in the session now follow, -1 for error
	 * @throws SQLException
	 */
	public static int addFollow(String follow_name, Session s)
			throws SQLException {// returns id of follow if added, -1 otherwise
									// first step-get follow id
		int follow_id = -1, user_id = -1;
		TableItem[] ti = new TableItem[1];
		ti[0] = new TableItem("name", Tools.Type.VARCHAR, follow_name);
		ResultSet follow_rs = null;
		try {
			s.getCon().getConnection().setAutoCommit(false);

			follow_rs = s.getCon().select("id", ti, null, null,
					JDBCConnection.Table.USER);
			if (Tools.isEmpty(follow_rs)) {// not in db. abort!

			} else {// in db.
				follow_rs.next();
				follow_id = follow_rs.getInt("id");
				user_id = s.getUser_id();
				// third step- find if not already following
				ti = new TableItem[2];
				ti[0] = new TableItem("idA", Tools.Type.INT, user_id);
				ti[1] = new TableItem("idB", Tools.Type.INT, follow_id);
				follow_rs = s.getCon().select("idA", ti, Tools.Connector.AND,
						null, JDBCConnection.Table.FOLLOWERSRELATIONSHIP);
				if (!Tools.isEmpty(follow_rs)){
					s.getCon().getConnection().commit();
					return follow_id;
				}
				// add relationship
				s.getCon().insert(JDBCConnection.Table.FOLLOWERSRELATIONSHIP,
						ti);
				java.util.Date today = new java.util.Date();
				HistoryModifier.addHistoryItem(s, follow_name,
						Tools.History.ADDEDFOLLOWER,
						new java.sql.Date(today.getTime()),-1);
				s.getCon().getConnection().commit();
				return follow_id;
			}
			s.getCon().getConnection().commit();
		} catch (SQLException e) {
			if (s.getCon().getConnection() != null)
				s.getCon().getConnection().rollback();

		} finally {
			if (follow_rs != null)
				follow_rs.close();
		}
		return follow_id;
	}

	/**
	 * 
	 * @param user_name
	 * @param follow_name
	 * @param s
	 * @return id of user which user in session do not follow from now, -1 for
	 *         error
	 * @throws SQLException
	 */
	public static int removeFollow(String follow_name, Session s)
			throws SQLException {// returns id of follow if deleted.
									// -1 otherwise
		int follow_id = -1, user_id = -1;
		TableItem[] ti = new TableItem[1];
		ti[0] = new TableItem("name", Tools.Type.VARCHAR, follow_name);
		ResultSet follow_rs = null;
		try {
			s.getCon().getConnection().setAutoCommit(false);
			follow_rs = s.getCon().select("id", ti, null, null,
					JDBCConnection.Table.USER);
			if (Tools.isEmpty(follow_rs)) {// not in db.
				return -1;
			} // in db.
				// second step- find user's id
			follow_rs.next();
			follow_id = follow_rs.getInt("id");
			user_id = s.getUser_id();
			// third step- find if following
			ti = new TableItem[2];
			ti[0] = new TableItem("idA", Tools.Type.INT, user_id);
			ti[1] = new TableItem("idB", Tools.Type.INT, follow_id);
			follow_rs = s.getCon().select("idA", ti, Tools.Connector.AND, null,
					JDBCConnection.Table.FOLLOWERSRELATIONSHIP);
			if (Tools.isEmpty(follow_rs))// not following
				return -1;
			// delete relationship
			s.getCon().delete(ti, Tools.Connector.AND,
					Table.FOLLOWERSRELATIONSHIP);
			s.getCon().getConnection().commit();
		} catch (SQLException e) {
			if (s.getCon().getConnection() != null)
				s.getCon().getConnection().rollback();
		} finally {
			if (follow_rs != null)
				follow_rs.close();
		}
		return follow_id;
	}

	public static List<String> getAllFollowed(Session s) throws SQLException {
		List<String> all = new ArrayList<String>();
		String follow_name;
		int uid = s.getUser_id();
		ResultSet follow_rs = null;
		try {
			s.getCon().getConnection().setAutoCommit(false);

			TableItem[] ti = new TableItem[1];
			ti[0] = new TableItem("idA", Tools.Type.INT, uid);
			follow_rs = s.getCon().select("idB", ti, null, null,
					Table.FOLLOWERSRELATIONSHIP);
			if (Tools.isEmpty(follow_rs))
				return all;
			while (follow_rs.next()) {
				follow_name = s.getCon().selectByName(follow_rs.getInt("idB"),
						Table.USER);
				if (follow_name != null)
					all.add(follow_name);
			}
			s.getCon().getConnection().commit();

		} catch (SQLException e) {
			if (s.getCon().getConnection() != null)
				s.getCon().getConnection().rollback();
		} finally {
			if (follow_rs != null)
				follow_rs.close();
		}
		return all;
	}
}
